package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.allservice.entity.Brand;
import com.cjq.allservice.entity.Type;
import com.cjq.allservice.entity.vo.BrandQuery;
import com.cjq.allservice.entity.vo.TypeQuery;
import com.cjq.allservice.service.BrandService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/allservice/brand")
@CrossOrigin
public class BrandController {


    @Autowired
    BrandService brandService;

    @GetMapping("/getAllBrand")
    public R getAllBrand(){
        List<Brand> list = brandService.list();
        return R.succ().data("brandList",list);
    }


    @PostMapping("/addBrand")
    public R addBrand(@RequestBody Brand brand){
        boolean save = brandService.save(brand);
        if (save==true){
            return R.succ().message("添加成功!");
        }else {
            return R.fail().message("添加失败！");
        }
    }


    @DeleteMapping("/deleteBrand")
    public R deleteBrand(@RequestParam String id){
        boolean b = brandService.removeById(id);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败！");
        }
    }
    @ApiOperation("获取品牌信息")
    @GetMapping("/getBrandById")
    public R getBrandById(@RequestParam String id){
        Brand brand = brandService.getById(id);
        return R.succ().data("brandInfo",brand);
    }

    @ApiOperation("条件查询品牌带分页")
    //条件查询带分页
    @PostMapping ("pageBrandCondition/{current}/{limit}")
    public R pageBrandCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) BrandQuery brandQuery){
        Page<Brand> page=new Page<>(current,limit);
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();

        //多条件组合查询
        String id = brandQuery.getBrandId();
        String name = brandQuery.getBrandName();
        String info = brandQuery.getBrandInfo();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("brand_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("brand_name",name);
        }
        if (!StringUtils.isEmpty(info)){
            wrapper.eq("brand_info",info);
        }
        wrapper.orderByDesc("update_time");

        brandService.page(page,wrapper);
        long total=page.getTotal();
        System.out.println(total);
        List<Brand> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.succ().data(map);
    }








}

